#include<queue>
#include<vector>
#include <utility>
using namespace std;
class Solution {
public:
    int dx[4] = { 0, 0, -1, 1 };
    int dy[4] = { 1, -1, 0, 0 };

    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
        queue<pair<int, int>>que;
        int src_color = image[sr][sc];
        if (src_color == color)return image;
        int n = image.size();
        int m = image[0].size();
        que.push({ sr,sc });
        while (que.size())
        {
            auto [a, b] = que.front();
            que.pop();
            image[a][b] = color;

            for (int i = 0; i < 4; i++)
            {
                int x = a + dx[i];
                int y = b + dy[i];
                if (x >= 0 && x < n && y >= 0 && y < m && image[x][y] == src_color)
                    que.push({ x,y });
            }
        }
        return image;
    }
};